/*
 * @Author: hongbin
 * @Date: 2023-10-12 13:29:15
 * @LastEditors: hongbin
 * @LastEditTime: 2023-10-17 18:00:03
 * @Description: 使用 mongoose 连接
 */
import mongoose from "mongoose";
import { ProjectManager } from "./Mongoose/Manager/Project.js";
import { PersonnelManager } from "./Mongoose/Manager/Personnel.js";
import { RoleManager } from "./Mongoose/Manager/Role.js";
import { Graph } from "./Mongoose/Manager/Graph.js";

const options = {
  /**
   * 本地使用127.0.0.1而不是localhost
   * 因为 Node.js 18 及更高版本更喜欢 IPv6 地址，
   * 这意味着，在许多计算机上，Node.js 将解析localhost为 IPv6 地址::1，
   * 而 Mongoose 将无法连接，除非 mongodb 实例在启用了 ipv6 的情况下运行。
   */
  host: "127.0.0.1",
  port: "27017",
  db_name: "work",
};

const MongoClientConfig = {
  auth: {
    username: "hongbin",
    password: "HelloHongbin",
  },
};
//连接规则： https://mongoosejs.com/docs/connections.html
const url = `mongodb://${options.host}:${options.port}/${options.db_name}`;

export class DbMongoose {
  constructor(app) {
    this.connect();
    this.ProjectManager = new ProjectManager(app);
    this.PersonnelManager = new PersonnelManager(app);
    this.RoleManager = new RoleManager(app);
    this.Graph = new Graph(app);
  }

  connect() {
    mongoose
      .connect(url, {
        user: MongoClientConfig.auth.username,
        pass: MongoClientConfig.auth.password,
      })
      .then(() => {
        console.log("连接成功🏅️");
      })
      .catch(err => {
        console.log("连接错误", err);
      });
  }
}
